package com.wgts.wgts.mvp.view.activity.goodsinfo;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.localbean.ProductImages;
import com.wgts.wgts.tools.MoHuGlide;

import java.util.ArrayList;

public class GoodInfoPictureAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{


    private static final int HEAD_TYPE = 0x00001;
    private static final int BODY_TYPE = 0x00002;

    private Context context;

    private ArrayList<ProductImages> scr;

    private String productTitle;

    public GoodInfoPictureAdapter(Context context, ArrayList<ProductImages> scr, String productTitle) {
        this.context = context;
        this.scr = scr;
        this.productTitle = productTitle;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if(i == HEAD_TYPE){
            return new GoodInfoDetailViewHolder(LayoutInflater.from(context).inflate(R.layout.product_detail_view_holder, viewGroup, false));
        }
        return new GoodInfoPictureViewHolder(LayoutInflater.from(context).inflate(R.layout.good_info_picture_view_holder, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if(viewHolder instanceof GoodInfoDetailViewHolder){
            ((GoodInfoDetailViewHolder) viewHolder).tvGoodsDetail.setText(productTitle);
        }else if(viewHolder instanceof GoodInfoPictureViewHolder){
            MoHuGlide.getInstance().glideImgsFormList(((GoodInfoPictureViewHolder) viewHolder).imgPicture, scr.get(i - 1).filePath, context);
        }
    }

    @Override
    public int getItemCount() {
        return scr.size() + 1;
    }

    @Override
    public int getItemViewType(int position){
        if (position == 0){
            return HEAD_TYPE;
        }else {
            return BODY_TYPE;
        }
    }

    public class GoodInfoPictureViewHolder extends RecyclerView.ViewHolder {
        public ImageView imgPicture;
        public GoodInfoPictureViewHolder(@NonNull View itemView) {
            super(itemView);
            imgPicture = itemView.findViewById(R.id.img_picture);
        }
    }

    public class GoodInfoDetailViewHolder extends RecyclerView.ViewHolder {
        public TextView tvGoodsDetail;
        public GoodInfoDetailViewHolder(@NonNull View itemView) {
            super(itemView);
            tvGoodsDetail = itemView.findViewById(R.id.tv_goods_detail);
        }
    }
}
